# ------------------------------
def get_topics_by_datatype(bag): 
# ------------------------------
# Copied from rxbag (just to make sure we have access to it)
    """ 
    Get all the message types in the bag and their associated topics.
    
    @param bag: bag file
    @type  bag: rosbag.Bag
    @return: mapping from message typename to list of topics
    @rtype:  dict of str to list of str
    """
    topics_by_datatype = {}
    for c in bag._get_connections():
        topics_by_datatype.setdefault(c.datatype, []).append(c.topic)
    
    return topics_by_datatype

# --------------------------------------
def performShuffle(toShuffle, seed=None):
# --------------------------------------
	# We want this ordering to be reproducable, so seed the random number generator 
	# with the *length* of the message list. That way as long as messages aren't added
	# to this topic the ordering will be reproducable
	import random
	if not seed:
		seed = len(toShuffle)
	random.seed(seed)

	# Do the shuffling
	random.shuffle(toShuffle)

	return toShuffle

# ----------------------------------------------
def get_message_times(bag, topic, shuffle=False):
# ----------------------------------------------
	connections = bag._get_connections(topics = [topic])
	msgTimes = [entry.time for entry in bag._get_entries(connections = connections)]

	# Optionally shuffle the message time list
	if shuffle:
		msgTimes = performShuffle(msgTimes)
	return msgTimes
